package com.atcjx.pojo.Query.Article;

import com.atcjx.MyUtils.wordSegUtils;
import com.atcjx.pojo.IConverter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.io.IOException;
import java.util.List;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName ArticleDTO.java
 * @Description TODO
 * @createTime 2021年09月26日 18:34:00
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
@ApiModel("文章查询的条件封装类")
public class ArticleQueryDTO  {
    @ApiModelProperty("封装查询文章的状态(多个请使用','分隔开)")
    private String status;
    @ApiModelProperty("封装查询文章的标签(多个请使用','分隔开)")
    private String tags;
    @ApiModelProperty("封装查询文章的主题")
    private String theme;
    @ApiModelProperty("封装查询文章的id")
    private String articleId;
    @ApiModelProperty("封装查询文章的标题")
    private String articleTitle;

    @ApiModelProperty("查询的信息")
    private String queryAll;
    private static class Converter implements IConverter<ArticleQueryDTO, ArticleQueryPo> {
        @Override
        public ArticleQueryPo convertToVo(ArticleQueryDTO articleQueryDTO) throws IOException {
            ArticleQueryPo articleQueryPo = new ArticleQueryPo();
            String status = articleQueryDTO.getStatus();
            String tags = articleQueryDTO.getTags();
            String queryAll=articleQueryDTO.getQueryAll();
            if (!"".equals(status) && status != null && !"null".equals(status)) {
                articleQueryPo.setStatus(status.split(","));
            }
            if (!"".equals(tags) && tags != null) {
                articleQueryPo.setArticleTags(tags.split(","));
                articleQueryPo.setArticleTagsLen(articleQueryPo.getArticleTags().length);
            }
            if(!"".equals(queryAll)&&queryAll!=null){
                articleQueryPo.setQueryAll(wordSegUtils.segment(queryAll));
                articleQueryPo.setQueryLength(articleQueryPo.getQueryAll().size());
                log.info("获取到的查询参数为{}", articleQueryPo.getQueryAll());
            }
            BeanUtils.copyProperties(articleQueryDTO, articleQueryPo);
            return articleQueryPo;
        }

        @Override
        public List<ArticleQueryPo> convertToVO(List<ArticleQueryDTO> t) {
            return null;
        }
    }
    /**
     * 描述：将DTO  转换为PO
     * @author 崔杰翔
     * @Date 2021/9/20 22:33
     * @return
     */
    public ArticleQueryPo   toArticleQueryPo(){
        try {
            return new Converter().convertToVo(this);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
